package com.adresvseti.wallpaperoid.helpers;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import com.adresvseti.wallpaperoid.models.Wallpaper;

import android.util.Xml;

public class WallpaperXmlParser {

	private static final String ns = null;
	
	private static final String ELEMENT_TAG = "ArrayOfWallpaperApiModel";
	private static final String ELEMENT_WALLPAPER = "WallpaperApiModel"; 
	private static final String ELEMENT_ID = "Id";
	private static final String ELEMENT_THUMB_URL = "ThumbUrl";
	
    public List<Wallpaper> parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            
            return readWallpapers(parser);
        } finally {
            in.close();
        }
    }
    
    private List<Wallpaper> readWallpapers(XmlPullParser parser) throws XmlPullParserException, IOException {
    	List<Wallpaper> wallpapers = new ArrayList<Wallpaper>();

        parser.require(XmlPullParser.START_TAG, ns, ELEMENT_TAG);
        while (parser.next() != XmlPullParser.END_TAG) {
            if (parser.getEventType() != XmlPullParser.START_TAG) {
                continue;
            }
            String name = parser.getName();
            // Starts by looking for the entry tag
            if (name.equals(ELEMENT_WALLPAPER)) {
                wallpapers.add(readWallpaper(parser));
            } else {
                skip(parser);
            }
        }
        
        return wallpapers;
    }
 
    
	 // Parses the contents of an entry. If it encounters a title, summary, or link tag, hands them off
	 // to their respective "read" methods for processing. Otherwise, skips the tag.
	 private Wallpaper readWallpaper(XmlPullParser parser) throws XmlPullParserException, IOException {

		 parser.require(XmlPullParser.START_TAG, ns, ELEMENT_WALLPAPER);

		 int id = -1;
	     String thumbUrl = null;

	     while (parser.next() != XmlPullParser.END_TAG) {
	         if (parser.getEventType() != XmlPullParser.START_TAG) {
	             continue;
	         }
	         String name = parser.getName();
	         if (name.equals(ELEMENT_ID)) {
	             id = readId(parser);
	         } else if (name.equals(ELEMENT_THUMB_URL)) {
	             thumbUrl = readThumbUrl(parser);
	         } else {
	             skip(parser);
	         }
	     }
	     return new Wallpaper(id, thumbUrl);
	 }

	 private int readId(XmlPullParser parser) throws IOException, XmlPullParserException {
	     parser.require(XmlPullParser.START_TAG, ns, ELEMENT_ID);
	     int value = Integer.parseInt(readText(parser));
	     parser.require(XmlPullParser.END_TAG, ns, ELEMENT_ID);
	     return value;
	 }
   
	 private String readThumbUrl(XmlPullParser parser) throws IOException, XmlPullParserException {
	    parser.require(XmlPullParser.START_TAG, ns, ELEMENT_THUMB_URL);
	    String value = readText(parser);
	    parser.require(XmlPullParser.END_TAG, ns, ELEMENT_THUMB_URL);
	    return value;
	}
 
	private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
	  String result = "";
	  if (parser.next() == XmlPullParser.TEXT) {
	      result = parser.getText();
	      parser.nextTag();
	  }
	  return result;
	}
    
	private void skip(XmlPullParser parser) throws XmlPullParserException, IOException {
	    if (parser.getEventType() != XmlPullParser.START_TAG) {
	        throw new IllegalStateException();
	    }
	    int depth = 1;
	    while (depth != 0) {
	        switch (parser.next()) {
	        case XmlPullParser.END_TAG:
	            depth--;
	            break;
	        case XmlPullParser.START_TAG:
	            depth++;
	            break;
	        }
	    }
	 }
}